//
//  LMFeedbackQuestion.m
//  MeetCard
//
//  Created by longmin on 16/12/6.
//  Copyright © 2016年 timtian. All rights reserved.
//

#import "LMFeedbackQuestion.h"
#import "LMOpaqueBtn.h"
#import "UILabel+Size.h"

@interface LMFeedbackQuestion ()

@property(nonatomic, strong) UILabel *questionLabel;

@property(nonatomic, strong) LMOpaqueBtn *likeBtn;

@property(nonatomic, strong) LMOpaqueBtn *normalBtn;

@property(nonatomic, strong) LMOpaqueBtn *hateBtn;

@property(nonatomic, weak) LMOpaqueBtn *selecteBtn;

@property(nonatomic, strong) UIView *lineView;

@end

@implementation LMFeedbackQuestion

#pragma mark - 生命周期

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.questionLabel];
        [self addSubview:self.likeBtn];
        [self addSubview:self.normalBtn];
        [self addSubview:self.hateBtn];
        [self addSubview:self.lineView];
        
        __weak typeof(self) weakSelf = self;
        void(^onClick)(LMOpaqueBtn *btn) = ^(LMOpaqueBtn *btn) {
            weakSelf.selecteBtn = btn;
        };
        
        self.likeBtn.onClick = self.normalBtn.onClick = self.hateBtn.onClick = onClick;
        onClick(self.likeBtn);
    }
    return self;
}

#pragma mark - Event

/** 反馈按钮点击 */
-(void)feedbackBtnClick:(LMOpaqueBtn *)feedBackBtn {
    
    self.likeBtn.selected = self.normalBtn.selected = self.hateBtn.selected = NO;
    feedBackBtn.selected = YES;
    if (self.feedTypeChangedBlock) {
        self.feedTypeChangedBlock(self.feedBackType);
    }
    
}

#pragma mark - Public

+ (instancetype)feedbackQuestionWithTitle:(NSString *)title {
    
    LMFeedbackQuestion *feedbackQuestion = [[LMFeedbackQuestion alloc] init];
    
    feedbackQuestion.width = __kScreenWidth;
    feedbackQuestion.questionLabel.text = title;
    
    [feedbackQuestion setLayout];
    feedbackQuestion.backgroundColor = [UIColor whiteColor];
    
    return feedbackQuestion;
}

#pragma mark - Private

-(void)setLayout {
    
    self.questionLabel.size = [self.questionLabel lm_sizeWithMargin:MK_PADDING_30 * __kwRatio];
    self.questionLabel.left = MK_PADDING_30 * __kwRatio;
    self.questionLabel.top = MK_PADDING_40 * __kwRatio;
    
    self.likeBtn.left = self.questionLabel.left;
    self.likeBtn.top = self.questionLabel.bottom + 28 * __kwRatio;
    
    self.normalBtn.top = self.likeBtn.top;
    self.normalBtn.centerX = self.width * 0.5;
    
    self.hateBtn.top = self.likeBtn.top;
    self.hateBtn.right = self.width - MK_PADDING_30 * __kwRatio;
    
    self.lineView.width = self.width - MK_PADDING_30 * 2 * __kwRatio;
    self.lineView.height = 0.5;
    self.lineView.centerX = self.width * 0.5;
    
    
    self.height = self.hateBtn.bottom + 8;
    self.lineView.bottom = self.height;
}

#pragma mark - 数据源和代理方法
#pragma mark - UIView -> 下面放继承自UIView的方法
#pragma mark - set方法和get方法

-(void)setSelecteBtn:(LMOpaqueBtn *)selecteBtn {
    _selecteBtn = selecteBtn;
    
    self.likeBtn.selected = self.normalBtn.selected = self.hateBtn.selected = NO;
    selecteBtn.selected = YES;
    
    if (selecteBtn == self.likeBtn) {
        self.feedBackType = LMFeedbackTypeLike;
    }else if (selecteBtn == self.normalBtn) {
        self.feedBackType = LMFeedbackTypeNormal;
    }else {
        self.feedBackType = LMFeedbackTypeHate;
    }
    
}

-(UILabel *)questionLabel {
    if (_questionLabel == nil) {
        _questionLabel = [[UILabel alloc] init];
        _questionLabel.font = _kFont32;
        _questionLabel.textColor = kColor3C3C3C;
    }
    return _questionLabel;
}

-(LMOpaqueBtn *)likeBtn {
    if (_likeBtn == nil) {
        _likeBtn = [LMOpaqueBtn opaqueBtnWithImage:@"icon_select_no" title:@"喜欢" font:_kFont32 space:MK_PADDING_30];
        [_likeBtn setImage:[UIImage imageNamed:@"icon_select_yes"] forState:UIControlStateSelected];
    }
    return _likeBtn;
}

-(LMOpaqueBtn *)normalBtn {
    if (_normalBtn == nil) {
        _normalBtn = [LMOpaqueBtn opaqueBtnWithImage:@"icon_select_no" title:@"一般" font:_kFont32 space:MK_PADDING_30];
        [_normalBtn setImage:[UIImage imageNamed:@"icon_select_yes"] forState:UIControlStateSelected];
    }
    return _normalBtn;
}

-(LMOpaqueBtn *)hateBtn {
    if (_hateBtn == nil) {
        _hateBtn = [LMOpaqueBtn opaqueBtnWithImage:@"icon_select_no" title:@"不喜欢" font:_kFont32 space:MK_PADDING_30];
        [_hateBtn setImage:[UIImage imageNamed:@"icon_select_yes"] forState:UIControlStateSelected];
    }
    return _hateBtn;
}

-(UIView *)lineView {
    if (_lineView == nil) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kColorD9D9D9;
    }
    return _lineView;
}

@end
